package 二叉树;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

//https://www.nowcoder.com/practice/91b69814117f4e8097390d107d2efbe0?tpId=295&tqId=23454&ru=/exam/intelligent&qru=/ta/format-top101/question-ranking&sourceUrl=%2Fexam%2Fintelligent
//https://leetcode.cn/problems/binary-tree-zigzag-level-order-traversal/?envType=featured-list&envId=q3iOID0B?envType=featured-list&envId=q3iOID0B
public class BM27_按之字形顺序打印二叉树 {

	public static void main(String[] args) {
		TreeNode node7= new TreeNode(7);
		TreeNode node6= new TreeNode(6);
		TreeNode node5= new TreeNode(5);
		TreeNode node4= new TreeNode(4);
		TreeNode node3= new TreeNode(3,node6,node7);
		TreeNode node2= new TreeNode(2,node4,node5);
		TreeNode node1= new TreeNode(1,node2,node3);
		
		
		BM27_按之字形顺序打印二叉树 test = new BM27_按之字形顺序打印二叉树();
		List<List<Integer>> res = test.zigzagLevelOrder01(node1);
		System.out.println(res.size());
		for(int i = 0;i < res.size();i++) {
			List<Integer> row = res.get(i);
			for(int j = 0;j < row.size();j++) {
				System.out.println(row.get(j));
			}
		}
	}
	
	public List<List<Integer>> zigzagLevelOrder01(TreeNode root) {
        List<List<Integer>> ans = new LinkedList<List<Integer>>();
        if (root == null) {
            return ans;
        }

        Queue<TreeNode> nodeQueue = new ArrayDeque<TreeNode>();
        nodeQueue.offer(root);
        boolean isOrderLeft = true;

        while (!nodeQueue.isEmpty()) {
            Deque<Integer> levelList = new LinkedList<Integer>();
            int size = nodeQueue.size();
            for (int i = 0; i < size; ++i) {
                TreeNode curNode = nodeQueue.poll();
                if (isOrderLeft) {
                    levelList.offerLast(curNode.val);
                } else {
                    levelList.offerFirst(curNode.val);
                }
                if (curNode.left != null) {
                    nodeQueue.offer(curNode.left);
                }
                if (curNode.right != null) {
                    nodeQueue.offer(curNode.right);
                }
            }
            while (!levelList.isEmpty()) {
            	System.out.print(levelList.poll()+",");
			}
            System.out.println();
            
            ans.add(new LinkedList<Integer>(levelList));
            isOrderLeft = !isOrderLeft;
        }
        return ans;
    }

	public List<List<Integer>> zigzagLevelOrder (TreeNode root) {
		List<List<Integer> > res = new ArrayList<>();
		if(root == null) return res;
		Queue<TreeNode> queue = new LinkedList<>();
		queue.offer(root);
		
		boolean isOrderLeft = true;
		while (!queue.isEmpty()) {
			List<Integer> row = new ArrayList<>();
			int size = queue.size();
			for(int i = 0;i<size;i++) {
				TreeNode node = queue.poll();
				row.add(node.val);
				if(node.left != null) {
					queue.offer(node.left);
				}
				if(node.right != null) {
					queue.offer(node.right);
				}
			}
			if(!isOrderLeft) {
				Collections.reverse(row);
			}
			isOrderLeft = !isOrderLeft;
			res.add(row);
		}
        return res;
    }

}
